/*************************************************************************
 * @file: undoredo.cpp
 * @date: 01/04/2012
 * TOPOL v1_0 - (c)copyright 2012 TEC/UFF
 *************************************************************************
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 ************************************************************************/

// includes
//----------
#include "undoredo.h"
#include "euleroper/euleroperator.h"
#include <iostream>


///////////////////////////////////////////////////
///  Constructor and Destructor Implementation  ///
///////////////////////////////////////////////////

//-------------------------------------------------------------------------
UndoRedo::UndoRedo()
{

}
//-------------------------------------------------------------------------



///////////////////////////////////////
///  PUBLIC Methods Implementation  ///
///////////////////////////////////////

//-------------------------------------------------------------------------
bool 
UndoRedo::hasUndo()
{
  if( m_undocommands.size() > 0 )
    return true;
  return false;
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
bool 
UndoRedo::hasRedo()
{
  if( m_redocommands.size() > 0 )
    return true;
  return false;
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void 
UndoRedo::insertCommand( EulerOperator* _command )
{
  //std::cout << "\nUndoRedo::insertCommand() \n";
  m_undocommands.push(_command);
  while( !m_redocommands.empty() )
    m_redocommands.pop();
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void 
UndoRedo::undo( int levels )
{
  for( int i = 1; i <= levels; i++ )
  {
    if( !m_undocommands.empty() )
    {
      EulerOperator* command = m_undocommands.top();
      command->unExecute();
      m_undocommands.pop();
      m_redocommands.push(command);
    }
  }
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void 
UndoRedo::redo( int levels )
{
  for( int i = 1; i <= levels; i++ )
  {
    if( !m_redocommands.empty() )
    {
      EulerOperator* command = m_redocommands.top();
      command->execute();
      m_redocommands.pop();
      m_undocommands.push(command);
    }
  }
}
//-------------------------------------------------------------------------

